package com.lfs.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.lfs.admin.controller.ShopController;
import com.lfs.admin.dao.ShopDao;
import com.lfs.admin.model.dto.ShopDto;
import com.lfs.admin.model.entity.ShopEntity;
import com.lfs.admin.model.entity.dataPackage.DataUploadEntity;
import com.lfs.admin.model.vo.ShopVo;
import com.lfs.admin.service.ShopService;
import com.lfs.admin.service.system.TokenService;
import com.lfs.base.util.StringUtils;
import com.lfs.common.constant.CommonConstants;
import com.lfs.common.core.domain.entity.SysUser;
import com.lfs.common.core.domain.model.LoginUser;
import com.lfs.common.utils.ServletUtils;
import com.lfs.dao.entity.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {

    private final Logger logger = LoggerFactory.getLogger(ShopServiceImpl.class);

    @Autowired
    private ShopDao shopDao;

    @Autowired
    private TokenService tokenService;

    @Override
    public PageBean<ShopEntity> queryShopList(ShopDto shopDto) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        String agtNo = StringUtils.isBlank(user.getUserName()) ? "error" : user.getUserName();
        shopDto.setAgtNo(agtNo);
        List<ShopEntity> shopEntityList = Lists.newArrayList();
        try {
            PageHelper.startPage(shopDto.getCurrentPage(), shopDto.getPageSize());
            shopEntityList  = shopDao.queryShopList(shopDto);

        } catch (Exception e) {
            logger.error("查询店铺列表异常:{}",e.getMessage(),e);
        }
        return new PageBean<ShopEntity>(shopEntityList);
    }

    @Override
    public List<ShopEntity> queryExpireShopList() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        String agtNo = StringUtils.isBlank(user.getUserName()) ? "error" : user.getUserName();
        return shopDao.queryExpireShopList(agtNo);
    }

    @Override
    public List<ShopEntity> queryUserShopList() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        String agtNo = StringUtils.isBlank(user.getUserName()) ? "error" : user.getUserName();
        return shopDao.queryUserShopList(agtNo);
    }

    @Override
    public int addShopInfo(ShopDto shopDto) {
        logger.info("执行新增店铺请求,shopId={},shopName={}",shopDto.getShopId(),shopDto.getShopName());
        // 判断当前新增的店铺名是否已经存在,如果已存在说明是重新授权, 更新店铺id,名称,accessToken有效期
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        String agtNo = StringUtils.isBlank(user.getUserName()) ? "error" : user.getUserName();
        ShopEntity shopEntity = shopDao.getShopByName(shopDto.getShopName());
        int count;
        if(null != shopEntity){
            shopEntity.setAccessToken(shopDto.getAccessToken());
            shopEntity.setExpiresIn(shopDto.getExpiresIn());
            shopEntity.setShopId(shopDto.getShopId());
            shopEntity.setRefreshToken(shopDto.getRefreshToken());
            shopEntity.setStatus(shopDto.getStatus());
            shopEntity.setShopLogo(shopDto.getShopLogo());
            shopEntity.setShopName(shopDto.getShopName());
            count = shopDao.updateShopInfo(shopEntity);
        }else{
            shopEntity = ShopEntity.builder()
                    .shopId(shopDto.getShopId())
                    .shopLogo(shopDto.getShopLogo())
                    .agtNo(agtNo)
                    .platformId(shopDto.getPlatformId())
                    .shopType(shopDto.getShopType())
                    .status(CommonConstants.SHOP_GRANT_NORMAL)
                    .accessToken(shopDto.getAccessToken())
                    .refreshToken(shopDto.getRefreshToken())
                    .expiresIn(shopDto.getExpiresIn())
                    .shopName(shopDto.getShopName())
                    .remark(shopDto.getRemark())
                    .build();
            count = shopDao.insertShop(shopEntity);
        }

        return count;
    }

    @Override
    public int deleteShop(int id) {
        return shopDao.deleteShop(id);
    }

    @Override
    public int updateShopInfo(ShopEntity shopEntity) {
        return 0;
    }

    @Override
    public List<ShopEntity> getCurrUserShopAllData() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        String agtNo = StringUtils.isBlank(user.getUserName()) ? "error" : user.getUserName();
        return shopDao.getCurrUserShopAllData(agtNo);
    }

    @Override
    public List<ShopEntity> queryShopListById(List<String> shopIds) {
        List<ShopEntity> shopEntityList = Lists.newArrayList();
        try {
            shopEntityList  = shopDao.queryShopListById(shopIds);
        } catch (Exception e) {
            logger.error("查询店铺列表异常:{}",e.getMessage(),e);
        }
        return shopEntityList;
    }

}
